<template>
  <div>
    <x-header>定时器{{timerIndex}}设置</x-header>
    <group>
      <datetime v-model="startTimeValue" format="HH:mm" @on-confirm="onConfirm" title="开始时间"></datetime>
      <!-- @on-change="log('cancel')" -->
      <!-- <datetime
        v-model="endTimeValue"
        format="HH:mm"
        @on-confirm="onConfirm"
        @on-change="log('cancel')"
        title='结束时间'
      ></datetime>-->

      <popup-picker
        title="操作"
        :data="modeList"
        v-model="mode"
        @on-show="onShow"
        @on-hide="onHide"
        @on-change="onChange"
        placeholder="please select"
      ></popup-picker>

      <cell title="开启" value="2019年02月14日14:36:48">
        <mt-switch v-model="isOn" onClick="event.cancelBubble = true" @change="turn"></mt-switch>
      </cell>
    </group>
    <x-button @click.native="submitAction">保存</x-button>
  </div>
</template>

<script>
import { getMYClient } from "../../js/client";
import { bus } from "../../js/client/bus";
// import { uint16ToBytes } from "../../js/hexUtil";
export default {
  name: "time-range-set",
  beforeRouteLeave(to, from, next) {
    // 设置下一个路由的 meta
    if (to.path == "/lightDetail") {
      to.meta.keepAlive = true; // B 跳转到 A 时，让 A 缓存，即不刷新
    }

    next();
  },
  methods: {
    submitAction() {
      var starDateTime = new Date();
      var start_mm_ss = this.startTimeValue.split(":");
      starDateTime.setHours(parseInt(start_mm_ss[0]));
      starDateTime.setMinutes(parseInt(start_mm_ss[1]));
      starDateTime.setSeconds(0);
      var startTimeStamp = parseInt(starDateTime.getTime() / 1000);

      var endDateTime = new Date();
      var end_mm_ss = this.endTimeValue.split(":");
      endDateTime.setHours(parseInt(end_mm_ss[0]));
      endDateTime.setMinutes(parseInt(end_mm_ss[1]));
      endDateTime.setSeconds(0);
      var endTimeStamp = parseInt(endDateTime.getTime() / 1000);

      console.log("rig time ", startTimeStamp + "~" + endTimeStamp);
      console.log("rig time ", starDateTime + "~" + endDateTime);

      var startTimeDat = uint16ToBytes(startTimeStamp);
      var endTimeDat = uint16ToBytes(endTimeStamp);

      var mode = new Uint8Array(1);
      if (this.mode[0] === "关闭") {
        mode[0] = 0;
      }
      if (this.mode[0] === "打开") {
        mode[0] = 1;
      }
      if (this.mode[0] === "闪烁") {
        mode[0] = 2;
      }

      if (this.mode[0] === "呼吸灯") {
        mode[0] = 3;
      }

      var enable = new Uint8Array(1);
      enable[0] = this.isOn ? 1 : 0;
      var dataBuffer = Buffer.concat([
        Buffer.from(startTimeDat),
        Buffer.from(endTimeDat),
        Buffer.from(mode),
        Buffer.from(enable)
      ]);

      var productId = this.$route.query.productId;
      var deviceId = this.$route.query.deviceId;
      var topic =
        "/" +
        productId +
        "/" +
        deviceId +
        "/task/" +
        this.timerIndex.toString() +
        "/set";
      getMYClient().mqtt_client.publish(topic, dataBuffer, 2);
      this.$router.go(-1);
    },
    turn() {},
    onChange(val) {
      console.log("val change", val);
      // this.mode = val[0]
    },
    cellAction: function() {
      alert("ddd");
    },
    onConfirm(val) {
      console.log("parm", this.$route.params);
    }
  },
  data() {
    return {
      startTimeValue: "00:00",
      endTimeValue: "00:00",
      timerIndex: 0,
      modeList: [["关闭", "打开", "闪烁", "呼吸灯"]],
      mode: ["关闭"],
      isOn: false
    };
  },
  mounted: function() {
    this.timerIndex = this.$route.params.timerIndex;
    var that = this
    bus.$on("msg", function(productId, deviceId, message, topic) {
      let topicCompoments = topic.split("/");
      let pId = topicCompoments[1];
      let dId = topicCompoments[2];
      let compoment_2 = topicCompoments[3];
      let compoment_4 = topicCompoments[4];

      if (
        dId === String(that.$route.query.deviceId) &&
        compoment_2 === "task"&&
        compoment_4 === String(that.timerIndex)
      ) {
        let data = new Uint8Array(message);
        var startTime = data.subarray(0, 4);
        var endTime = data.subarray(4, 8);
        var start_timestamp = bytesToUint(startTime);
        var end_timestamp = bytesToUint(endTime);
        var mode = data.subarray(8, 9);
        var enable = data.subarray(9, 10);


        if (mode[0] == 0x00) {
           that.mode = ["关闭"];
        }else if (mode[0] == 0x01) {
          that.mode = ["打开"];
        }else if (mode[0] == 0x02) {
          that.mode = ["闪烁"];
        } else if (mode[0] == 0x03) {
          that.mode = ["呼吸灯"];
        } 

        if(enable[0] == 1){
          that.isOn = true
        }else{
          that.isOn = false
        }


        var start_date = new Date(start_timestamp * 1000);
        that.startTimeValue  = String(start_date.getHours()) + ":" + String(start_date.getMinutes())




      }

      console.log(message + "哈哈哈哈哈哈啊哈哈哈");
    });

    
    var productId = this.$route.query.productId;
    var deviceId = this.$route.query.deviceId;
    var topic =
      "/" +
      String(productId) +
      "/" +
      String(deviceId) +
      "/task/" +
      String(this.timerIndex) +
      "/get";
    getMYClient().mqtt_client.publish(topic, "hahaha", 2);
  }
};
</script>

<style scoped>
</style>
